﻿using System;
using SmartMenu.Classes;
using SmartMenu.Controllers;

public partial class Admin_Pages__Menu_Menu_Menu : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["MenuId"] != null)
        {
            int menuid = 0;
            if(Int32.TryParse(Request.QueryString["MenuId"], out menuid))
            {
                MenuInfo menu = MenuController.GetMenu(menuid);
                if (menu != null)
                {
                    NAME.Text = menu.Name;
                }
            }
        }
    }
    protected void SAVE_Click(object sender, EventArgs e)
    {
        if (Request.QueryString["MenuId"] != null)
        {
            int menuid = 0;
            if (Int32.TryParse(Request.QueryString["MenuId"], out menuid))
            {
                MenuInfo menu = MenuController.GetMenu(menuid);
                if (menu != null)
                {
                    if (!string.IsNullOrEmpty(NAME.Text))
                    {
                        menu.Name = NAME.Text;
                        MenuController.UpdateMenu(menu);
                    }                    
                }
            }
        }
        else
        {
            if (!string.IsNullOrEmpty(NAME.Text))
            {
                MenuController.AddMenu(new MenuInfo(NAME.Text));
            }        
        }
    }
    protected void CANCEL_Click(object sender, EventArgs e)
    {
        Response.Redirect(Request.RawUrl);
    }
}